using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Portal : Prop
{
    [ColorUsageAttribute(true, true)]
    public Color baseColor;
    public Portal linkPortal;
    PlayerController player;
    GameObject sphereObj;
    bool canUse = true;

    protected override void Awake()
    {
        base.Awake();
        player = FindObjectOfType<PlayerController>();
        GameObject spherePrefab = Resources.Load<GameObject>("Sphere");
        sphereObj = Instantiate(spherePrefab);
        sphereObj.transform.position = transform.position;
        sphereObj.transform.localScale = Vector3.one;
        sphereObj.SetActive(false);
    }

    protected override void Start()
    {
        base.Start();
        GetComponentInChildren<Renderer>().material.SetColor("Color_", baseColor);
    }

    private void OnTriggerEnter(Collider other)
    {
        if(!canUse) return;
        if (other.tag != "Player") return;
        if (!linkPortal.gameObject.activeSelf) return;
        if(!linkPortal.canUse) return;
        if (player.canMoveBlock.IsOverlap) return;
        if (player.transform.localScale.x <= 0.99f) return;
        if(LevelManager.instance.cameraMoving) return;
        if(player.IsPushing()) return;

        StartCoroutine(PortalCoroutine());
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.tag != "Player") return;
        canUse = true;
    }

    IEnumerator PortalCoroutine()
    {
        GameManager.instance?.PlayPlayerAudio(true);

        MessageManager.Run(MsgKey.Portaling, true);
        player.SetNavEnable(false);
        while(player.transform.localScale.x>0)
        {
            player.transform.localScale -= Time.deltaTime * Vector3.one * 3;
            player.transform.position += Vector3.up * Time.deltaTime * 3 * 1.5f;
            yield return null;
        }
        player.transform.localScale = Vector3.zero;
        player.transform.position = linkPortal.transform.position;

        sphereObj.SetActive(true);
        Vector3 dir = (linkPortal.transform.position - transform.position).normalized;
        sphereObj.transform.position = transform.position;
        while (Vector3.Distance(linkPortal.transform.position, sphereObj.transform.position)>0.5f)
        {
            sphereObj.transform.Translate(dir * Time.deltaTime * 15);
            yield return null;
        }
        sphereObj.SetActive(false);

        GameManager.instance?.PlayPlayerAudio(false);

        while (player.transform.localScale.x<1)
        {
            player.transform.localScale += Time.deltaTime * 4 * Vector3.one;
            player.transform.position -= Vector3.up * Time.deltaTime * 3 * 1.5f;
            yield return null;
        }
        player.transform.localScale = Vector3.one;
        player.SetNavEnable(true);
        player.canMoveBlock.FindStander();
        MessageManager.Run(MsgKey.Portaling, false);
    }

    protected override void MoveToRight()
    {
        base.MoveToRight();

        Ray ray1 = new Ray(transform.position, Vector3.left);
        Ray ray2 = new Ray(transform.position, Vector3.right);

        if (Physics.Raycast(ray1, out RaycastHit hit, 100, ~(1 << 0)) || Physics.Raycast(ray2, 100, ~(1 << 0)))
        {
            canUse = false;
        }
    }

    protected override void MoveToForward()
    {
        base.MoveToForward();

        Ray ray1 = new Ray(transform.position, Vector3.forward);
        Ray ray2 = new Ray(transform.position, Vector3.back);

        if (Physics.Raycast(ray1, out RaycastHit hit, 100, ~(1<< 0)) || Physics.Raycast(ray2, 100, ~(1 << 0)))
        {
            canUse = false;
        }
    }

    protected override void MoveToOrig()
    {
        base.MoveToOrig();
        canUse = true;
    }
}
